package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.controller
 */
@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 查询所有的班级列表
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("clazz = {}", clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    /**
     * 分页查询班级
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result pageQuery(ClazzQueryParam clazzQueryParam){
        PageResult<Clazz> pageResult = clazzService.pageQuery(clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(clazzService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        clazzService.updateById(clazz);
        return Result.success();
    }


}
